package com.dpkit.adapter.orm.impl.jpa;

import com.dpkit.adapter.DatabaseDialect;
import com.dpkit.adapter.OrmAdapter;
import com.dpkit.adapter.orm.definition.EntityDefinition;
import jakarta.persistence.EntityManagerFactory;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

public class JpaAdapter implements OrmAdapter<Object> {
    private final EntityManagerFactory entityManagerFactory;
    private final DatabaseDialect dialect;

    @Override
    public void save(String entityName, Map<String, Object> data) {

    }

    @Override
    public void batchSave(String entityName, List<Map<String, Object>> batchData) {

    }

    @Override
    public Class<Object> generateEntityClass(EntityDefinition definition) {
        DynamicType.Builder<Object> builder = new ByteBuddy()
            .subclass(Object.class)
            .name(definition.getFullClassName())
            .annotateType(Entity.class)
            .annotateType(Table.class.getConstructor(String.class), definition.getTableName());

        // 添加字段和注解
        definition.getFields().forEach((name, field) -> {
            builder = builder.defineField(name, field.getType(), Modifier.PRIVATE)
                .annotateField(createColumnAnnotation(field));
            
            if (field.isId()) {
                builder = builder.annotateField(AnnotationDescription.Builder.ofType(Id.class).build())
                    .annotateField(AnnotationDescription.Builder.ofType(GeneratedValue.class)
                        .define("strategy", GenerationType.IDENTITY).build());
            }
        });

        return builder.make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded();
    }

    @Override
    public DataSource getDataSource() {
        return null;
    }

    // 其他实现方法...
}